package qzy.security;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import qzy.dao.ISysUserDao;
import qzy.entity.SysUser;

@Service
public class SysUserDetailsService implements UserDetailsService {

	@Autowired
	ISysUserDao userDao;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

		// 用户
		SysUser sysUser = userDao.getUserByName(username);

		// 角色
		Collection<GrantedAuthority> grantedList = new ArrayList<>();
		SimpleGrantedAuthority granted = new SimpleGrantedAuthority(sysUser.getUserrole());
		grantedList.add(granted);

		return new SysUserDetails(sysUser, grantedList);
	}

}
